const {exec} = require("child_process");

function _HandleOutput(callback, stdout, error, stderr) {
    if(error) {
        console.log(`error: ${error.message}`);
        callback(false, error.message);
        return;
    }
    if(stderr) {
        console.log(`stderr: ${stderr}`);
        callback(false, stderr);
        return;
    }
    console.log(`stdout: ${stdout}`);
    callback(true, stdout);
}
function GitCommit(msg, callback) {
    exec(`git commit -m "${msg}"`, (error, stdout, stderr) => {
        _HandleOutput(callback, stdout, error, stderr);
    });
}

function GitAdd(callback) {
    const _cmd = `cd client && git add -A && cd ..`;
    exec(_cmd, (error, stdout, stderr) => {
        _HandleOutput(callback, stdout, error, stderr);
    });
}

function GitPush(name, branch, callback) {
    exec(`git push ${name} ${branch}`, (error, stdout, stderr) => {
        _HandleOutput(callback, stdout, error, stderr);
    });
}

function GitPull(name, branch, callback) {
    exec(`git pull ${name} ${branch}`, (error, stdout, stderr) => {
        _HandleOutput(callback, stdout, error, stderr);
    });
}

function GitLog(callback) {
    const _cmd = `cd client && git log --oneline -20 && cd ..`;
    exec(_cmd, (error, stdout, stderr) => {
        _HandleOutput(callback, stdout, error, stderr);
    });
}

function RebuildClient() {
    const _cmd = `cd client && node build.js && cd ..`;
    exec(_cmd, (error, stdout, stderr) => {
        _HandleOutput(callback, stdout, error, stderr);
    });
}



/// https://github.com/kpdecker/jsdiff
module.exports = Object.freeze({GitPush, GitAdd, GitCommit, GitPull, RebuildClient, GitLog});
